home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Science / RLaB / rlib / diff.r < prev    next >
Text File  |  1994-04-25  |  814b  |  40 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax:    diff ( X )
  4. //        diff ( X , k )
  5.  
  6. //  Description:
  7.  
  8. //  The function diff computes the difference between adjacent
  9. //  elements in a vector. The function diff merely does:
  10.  
  11. //    x = x[2:n] - x[1:n-1];
  12.  
  13. //  If X is a matrix, then the operation is performed on each column
  14. //  of X.
  15.  
  16. //  The default value for `k' is 1. If k is specified as other than 1,
  17. //  then the operation is performed k times.
  18.  
  19. //-------------------------------------------------------------------//
  20.  
  21. diff = function ( X, k )
  22. {
  23.   local (i, m, n, x);
  24.  
  25.   if (!exist (k)) { k = 1; }
  26.  
  27.   x = X;
  28.   for (i in 1:k)
  29.   {
  30.     m = size(x)[1]; n = size(x)[2];
  31.     if (m == 1)
  32.     {
  33.       x = x[2:n] - x[1:n-1];
  34.     else
  35.       x = x[2:m;] - x[1:m-1;];
  36.     }
  37.   }
  38.   return x;
  39. };
  40.